package tasks 
{
	import app.AppModel;
	import app.textures.TextureInfo;
	import app.textures.TextureInfoGroupe;
	import app.textures.TexturePartInfo;
	import flash.events.Event;
	import utils.Converter;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class LoadTexture extends BaseXmlLoadTask
	{
		
		private var _textureGroupe:TextureInfoGroupe;
		private var _loadersParts:Vector.<LoadTextureParts>;
		private var _taskCompleted:int;
		private var _allTaskCompleted:Boolean;
		
		public function LoadTexture(xmlFile:String, textureGroupe:TextureInfoGroupe) 
		{
			super(xmlFile);
			_textureGroupe = textureGroupe;
		}
		
		override public function run():void 
		{
			if (_textureGroupe == null)
			{
				handleError("Texture groupe is null");
				complete(false);
				return;
			}
			super.run();
		}
		
		override protected function onXMLReaded(xml:XML):void 
		{
			var xmlList:XMLList = xml.texture;
			var count:int = xmlList.length();
			
			var texture:TextureInfo;
			if (_textureGroupe.textureType ==  TextureInfoGroupe.TEXTURE_TYPE_SET_OF_BITMAP)
			{
				_loadersParts = new Vector.<LoadTextureParts>();
			}
			
			try
			{
				for (var i:int =0; i < count; i++)
				{
					texture = new TextureInfo(xmlList[i].attribute("title"));
					texture.previewURL = xmlList[i].attribute("preview");
					texture.color = getColor(xmlList[i].attribute("color"));
					texture.sourceURL = xmlList[i].attribute("source");
					//texture.id = xmlList[i].attribute("id");
					_textureGroupe.addTexture(texture);
					if (_textureGroupe.textureType ==  TextureInfoGroupe.TEXTURE_TYPE_SET_OF_BITMAP)
					{
						_loadersParts.push(new LoadTextureParts(texture.sourceURL, texture));
					}
				}
			}catch(err:Error)
			{
				handleError("Error while XML: " + err.errorID + "; " + err.name + "; "  + err.message);
				complete(false);
				return;
			}
			
			if (_loadersParts != null && _loadersParts.length > 0)
			{
				// run tasks
				_taskCompleted = 0;
				_allTaskCompleted = true;
				for each(var taskForRun:BaseTask in _loadersParts)
				{
					taskForRun.addEventListener(Event.CHANGE, handleTaskChange);
					taskForRun.run();
				}
				return;
			}
			
			// no parts for load -> complete
			complete(true);
		}
		
		private function handleTaskChange(e:Event):void 
		{
			_taskCompleted++;
			var task:BaseTask = e.currentTarget as BaseTask;
			if (task != null)
			{
				task.removeEventListener(Event.CHANGE, handleTaskChange);
				if (!task.ready)
				{
					_allTaskCompleted = false;
				}
			}else
			{
				handleError("Completed task is null");
			}
			
			if (_taskCompleted == _loadersParts.length)
			{
				complete(_allTaskCompleted);
			}
		}
		
		private function getColor(colorXML:String):uint
		{
			var color:uint;
			if (colorXML.length == 0)
			{
				color = 0x000000;
			}else
			{
				color= uint(Converter.getHexColor(colorXML));
			}
			if (isNaN(color))
			{
				handleError("Wrong color format: " + colorXML);
				color = 0xFF0000;
			}
			return color;
		}
		
		override protected function handleError(err:String):void 
		{
			super.handleError("Load textures: " + err);
		}
		
		override public function destroy():void 
		{
			_textureGroupe = null;
			if (_loadersParts != null)
			{
				for each(var task:BaseTask in _loadersParts)
				{
					task.removeEventListener(Event.CHANGE, handleTaskChange);
					task.destroy();
				}
				_loadersParts.splice(0, _loadersParts.length);
			}
			_loadersParts = null;
			super.destroy();
		}
		
	}

}